<?php
namespace app\admin\service;
use think\Db;
class  Account{
    //管理员列表
    function adminList(){
        $Admin = session('Admin_user_lvtuan');
        $admin = Db::name('admin');
       if($Admin['a_role'] != 2){
            $where['a.a_role']=array('neq','2');
        }else{
            $where = null;
        }
        $join = [
            ['lt_admin_role b','a.a_role=b.role_id'],
        ];
        $data['list']=$admin->alias('a')->join($join)->where($where)->order('a.a_addtime desc')->select();
        return $data;
    }



    //管理员查询信息，
    function adminFind($admin_id){
        $data = Db::name('admin')->where('admin_id',$admin_id)->field('admin_id,a_fullname,a_role,a_phone,a_img')->find();
        return $data;
    }

    //管理员新增
    function adminAddApi($parr){
        $admin = Db::name('admin');
        $va_phone = preg_match('/^1\d{10}$/', $parr['a_phone']);
        if (!$va_phone) {
            return messageInfo('10008', '手机号码格式错误');
        }
        if(empty($parr['admin_id'])){
            if(!preg_match('|^[0-9a-zA-Z]+$|', $parr['a_username']) || strlen($parr['a_username']) < 6 || strlen($parr['a_username']) > 16  ){
                $response = messageInfo('10008','账号必需输入6~16位字母或数字组合');
                return $response;
            }
          /*  $password = preg_match('/(?=.*[a-z])(?=.*\d)(?=.*[#@!~%^&*])[a-z\d#@!~%^&*]{8,16}/i', $parr['a_password']);*/
            if(!preg_match("/^[A-Za-z0-9]+$/",$parr ['a_password'])){
                return messageInfo('10008', '密码必需为数字、字母组合！');
            }
            if(strlen($parr['a_password']) < 6 || strlen($parr['a_password']) > 16 ){
                return messageInfo('101', '密码只允许设置6~16位!');
            }
            $where['a_username'] = $parr['a_username'];
            $a_username          = $admin->where($where)->value('a_username');
            if(!empty($a_username)){
                $response = messageInfo('10008','账号已存在');
                return $response;
            }
            $arr['a_username']  =   $parr['a_username'];
            $arr['a_password']  =   md5($parr['a_password']);
            $arr['a_addtime']   =   Date('Y-m-d H:i:s');
            if($parr['a_role'] == 2 || $parr['a_role'] == 3){
                return messageInfo('10008', '系统默认角色，不能新增与编辑');
            }
        }
        if(!empty($parr['a_img'])){
            $arr['a_img']   =   $parr['a_img'];
        }
        $arr['a_role']      =   $parr['a_role'];
        $arr['a_phone']     =   $parr['a_phone'];
        $arr['a_fullname']  =   $parr['a_fullname'];
        if(empty($parr['admin_id'])){
            $b=$admin->insert($arr);
            $return_name = '新增';
        }else{
            $arr['admin_id']  =   $parr['admin_id'];
            $return_name = '编辑';
            $b=$admin->update($arr);
        }
        if($b){
            $url      = config('tempredirect_url').'adac/index';
            $response = messageInfo('11000',$return_name.'成功','',$url);
        }else{
            $response = messageInfo('10008',$return_name.'失败');
        }
        return $response;
    }

    //查询管理员信息
    function AdminDemand($admin_id){
        $admin = Db::name('admin');
        $where['admin_id'] = $admin_id;
        $data =$admin->where($where)->find();
        $data['r_username'] = Db::name('admin_role')->where('role_id='.$data['a_role'])->value('r_username');
        return $data;
    }

    //管理员删除t
    function adminDeleteApi($admin_id){
        $admin = Db::name('admin');
        $admin->startTrans();//开户事务
        $where['admin_id']=$admin_id;
        $b=$admin->where($where)->delete();
        $log['ao_record']="删除管理员";
        $b_1=addOperation($log);
        if(!$b_1){
            $admin->rollback();
            $response = messageInfo('10008','删除失败');
            return $response;
        }
        if($b){
            $admin->commit();
            $response = messageInfo('11000','删除成功');
        }else{
            $admin->rollback();
            $response = messageInfo('10008','删除失败');
        }
        return $response;
    }

    //密码修改
    function adminChangeApi($parr){
        $admin = Db::name('admin');
        $admin->startTrans();//开户事务
        if(!preg_match('|^[0-9a-zA-Z]+$|', $parr['userpassword']) || !preg_match('|^[0-9a-zA-Z]+$|', $parr['userpassword2'])){
            $response = messageInfo('10008','必需输入字母或数字');
            return $response;
        }
        if(empty($parr['admin_id'])){
            $response = messageInfo('10008','参数错误');
            return $response;
        }
        if($parr['userpassword'] != $parr['userpassword2']){
            $response = messageInfo('10008','新密码与确认密不一致');
            return $response;
        }
        $userpassword2      = md5($parr['userpassword2']);
        $where['admin_id']  = $parr['admin_id'];
        $b  =$admin->where($where)->setField('a_password',$userpassword2);
        if(!$b){
            $admin->rollback();
            $response = messageInfo('10008','亲密码与旧密码一致');
            return $response;
        }
        $log['ao_record']="修改-".$parr['admin_id'].'的密码';
        $b_1=addOperation($log);
        if(!$b_1){
            $admin->rollback();
            $response = messageInfo('10008','修改失败');
            return $response;
        }
        if($b){
            $admin->commit();
            $url      = config('tempredirect_url').'adac/index';
            $response = messageInfo('11000','修改成功','',$url);
        }else{
            $admin->rollback();
            $response = messageInfo('10008','修改失败');
        }
        return $response;
    }

    //权限列表
    function jurisdictionList($a_role){
        $Admin_user_lvtuan = session('Admin_user_lvtuan');
       if($Admin_user_lvtuan['a_role'] !=2){
            $join = [
                ['lt_admin_role_function b','a.admin_function_id=b.admin_function_id'],
            ];
             $wher['b.admin_role_id']  = 3;

        }
        $where['am_pid'] = 0;
        $Model_2         = Db::table('lt_admin_menu')->where($where)->select();

        foreach ($Model_2 as $k=>$v){
            $wher['a.af_menuid'] = $v['admin_menu_id'];
            if($Admin_user_lvtuan['a_role'] !=2){
                $Model_2[$k]['son'] = Db::name('admin_function')->alias('a')->join($join)->where($wher)->field('a.*')->select();
            }else{
                $Model_2[$k]['son'] = Db::name('admin_function')->alias('a')->where($wher)->field('a.*')->select();
            }
            foreach ($Model_2[$k]['son'] as $key => $value) {
                $where_1['af_strte']    = 1;
                $where_1['af_menuid']=$value['admin_menu_id'];
                $Model_2[$k]['son'][$key]['Grandson'] = Db::name('admin_function')->where($where_1)->select();
            }
        }

        $data['model_2'] = $Model_2;
        //当前角色拥有权限
        $data['result'] = Db::name('admin_role_function')->where('admin_role_id='.$a_role)->field('admin_function_id')->select();
        $data['r_username'] = Db::name('admin_role')->where('role_id='.$a_role)->value('r_username');
        return $data;
    }


    //权限分配
    function permissionDistributionApi($admin_function_id,$admin_role_id){
        $Model = Db::name('admin_role_function');
        $Model->startTrans();//开启事物
        $where_a['admin_role_id'] = $admin_role_id;
        $count = $Model->where($where_a)->count();
        if($count > 0){
            $del = $Model->where($where_a)->delete();
            if(!$del){
                $Model->rollback();
                $response = messageInfo('10008','原来权限删除失败');
                return $response;
            }
        }

        foreach ($admin_function_id as $key => $value){
                $data = array(
                    'admin_role_id'     => $admin_role_id,
                    'admin_function_id' =>$value
                );
                $b=$Model->insert($data);
                if(!$b){
                    $Model->rollback();
                    return  $response = messageInfo('10008','分配失败');die;
                }
        }
        $Model->commit();//成功刚提交
        $response = messageInfo('11000','分配成功');
        return $response;
    }




    //操作日志列表
    function managerlogList($where=''){
        $field="a.*,b.a_fullname,b.a_role";
        $join = [
            ['lt_admin b','a.admin_id=b.admin_id'],
        ];
        $paginate = 40;
        $data['list']=Db::table('lt_admin_operation')
            ->alias('a')
            ->join($join)
            ->where($where)
            ->field($field)
            ->order('ao_addtime desc')
            ->paginate($paginate);
        $lists_data = $data['list']->toArray();
        foreach ($lists_data['data'] as $k=>$v){
            $lists_data['data'][$k]['adminNa'] = Db::table('lt_admin_role')->where('role_id = '.$v['a_role'] )->value('r_username');
        }
        $page =  $data['list']->render();

        if(!empty($where)){
            $usen_name= Db::name('admin')->alias('a')->where($where)->value('a_fullname');
            $data['user_name'] = $usen_name;
        }
        $data['list'] = $lists_data['data'];
        $data['page']= $page;// 分页显示输出*/
        return $data;
    }


    //后台右上角统计
    function rightCount(){
        $price=Date('Y-m-d');
        $where['DATE_FORMAT(o_addtime,"%Y-%m-%d")'] = array('eq',$price);

        $data['order']=M('orders')->where($where)->count();

        $where_1['DATE_FORMAT(u_addtime,"%Y-%m-%d")'] = array('eq',$price);
        $data['user']=M('user')->where($where_1)->count();
        return $data;
    }

    //菜单列表数据
    function menusList($where=''){
        $admin_menu = Db::name('admin_menu');
        $data=$admin_menu->where($where)->order('am_sort asc')->select();
        return $data;
    }


    //菜单编辑 数据
    function menusInfo($where){
        $admin_menu = Db::name('admin_menu');
        $data=$admin_menu->where($where)->find();
        return $data;
    }

    //菜单添加
    function menusAddApi($parr){
        $admin_menu = Db::name('admin_menu');
        $arr['am_mname'] = $parr['am_mname'];
        $arr['am_murl']  = $parr['am_murl'];
        $arr['am_sort']  = $parr['am_sort'];
        if(!empty($parr['admin_menu_id'])){
            $arr['admin_menu_id']=$parr['admin_menu_id'];
            $b  = $admin_menu->update($arr);
            if($b !== false){
                $url      = config('tempredirect_url').'adac/menus';
                $response = messageInfo('11000','编辑成功','',$url);
            }else{
                $response = messageInfo('10008','编辑失败');
            }
        }else{
            $b = $admin_menu->insert($arr);
            if($b){
                $url      = config('tempredirect_url').'adac/menus';
                $response = messageInfo('11000','添加成功','',$url);
            }else{
                $response = messageInfo('10008','添加成功');
            }
        }
        return $response;
    }


    //子菜单添加
    function sonmenusAddApi($parr){
        $admin_menu = Db::table('lt_admin_menu');
        $adminFunction = Db::name('admin_function');
        $admin_menu->startTrans();//开启事物
        $arr['am_mname']  = $parr['am_mname'];
        $arr['am_murl']   = $parr['am_murl'];
        $arr['am_sort']   = $parr['am_sort'];
        if(!empty($parr['admin_menu_id'])){
            $where['admin_menu_id'] = $parr['admin_menu_id'];
            $b=$admin_menu->where($where)->update($arr);
            $arr_1=array(
                'af_fname' => $parr['am_mname'],
                'af_furl'  => $parr['am_murl']
            );
            $b_1=$adminFunction->where($where)->update($arr_1);
            if($b !== false && $b_1 !== false){
                $admin_menu->commit();//成功刚提交
                $url      = config('tempredirect_url').'adac/sonmenus/am_pid/'.$parr['am_pid'];
                $response = messageInfo('11000','编辑成功','',$url);
            }else{
                $admin_menu->rollback();//失败则回滚
                $response = messageInfo('10008','编辑失败');
            }
        }else{
            $arr['am_pid'] = $parr['am_pid'];
            $b = $admin_menu->insertGetId($arr);
            $array=array(
                'af_fname'  => $parr['am_mname'],
                'af_furl'   => $parr['am_murl'],
                'af_menuid' => $parr['am_pid'],
                'admin_menu_id' => $b
            );
            $b_1 = $adminFunction->insert($array);
            if($b && $b_1){
                $admin_menu->commit();//成功刚提交
                $url      = config('tempredirect_url').'adac/sonmenus/am_pid/'.$parr['am_pid'];
                $response = messageInfo('11000','添加成功','',$url);
            }else{
                $admin_menu->rollback();//失败则回滚
                $response = messageInfo('10008','添加失败');
            }
        }
        return $response;
    }


    //菜单删除
    function sonmenusDeleteApi($where){
        $admin_menu = Db::name('admin_menu');
        $b=$admin_menu->where($where)->delete();
        if($b){
            $response = messageInfo('11000','删除成功');
        }else{
            $response = messageInfo('10008','删除失败');
        }
        return $response;
    }


    //功能列表
    function featureData(){
        $db = Db::table('lt_admin_menu');
        $paginate = 40;
        $info = $db->where('am_pid = 0')->paginate($paginate);
        $number = 1;
        $lists_data = $info->toArray();
        foreach ($lists_data['data'] as $k=>$v){
            $lists_data['data'][$k]['child'] = Db::table('lt_admin_menu')->where("am_pid = ".$v['admin_menu_id'])->select();
            $lists_data['data'][$k]['number'] = $number;
            $number++;
        }
        $page         =  $info->render();
        $data['list'] = $lists_data['data'];
        $data['page'] = $page;// 分页显示输出*/
        return  $data;
    }



    //一级、二级功能
    function juniorData(){
        $db = Db::name('admin_menu');
        $admin_menu_id=trim(input('admin_menu_id'));
        $paginate = 40;
        $info = $db->where('am_pid = '.$admin_menu_id)->paginate($paginate);
        $number = 1;
        $lists_data = $info->toArray();
        foreach ($lists_data['data'] as $k=>$v){
            $lists_data['data'][$k]['child_two'] = Db::table('lt_admin_function')->where("af_menuid =".$v['admin_menu_id'])->field('af_fname')->select();
            $lists_data['data'][$k]['number']    = $number;
            $number++;
        }
        $page         =  $info->render();
        $data['list'] = $lists_data['data'];
        $data['page'] = $page;// 分页显示输出*/
        return  $data;
    }

    //三级功能详情
    function fudetailsData($where){
        $admin_function = Db::name('admin_function');
        $data=$admin_function->where($where)->select();
        return $data;
    }


    //三级功能添加
    function functionAddApi($parr){
        $db = Db::name('admin_function');
        if(!empty($parr['admin_function_id'])){
            $arr=array(
                'af_fname' => $parr['af_fname'],
                'af_furl'  => $parr['af_furl'],
                'admin_menu_id' => $parr['admin_menu_id'],
                'af_strte' => $parr['af_strte']
            );

            $where['admin_function_id']=$parr['admin_function_id'];
            $b=$db->where($where)->update($arr);
            if($b !== false ){
                $url      = config('tempredirect_url').'adac/junior/admin_menu_id/'.$parr['am_pid'];
                $response = messageInfo('11000','编辑成功','',$url);
            }else{
                $response = messageInfo('10008','编辑失败');
            }
        }else{
            $arr=array(
                'af_fname'  => $parr['af_fname'],
                'af_furl'   => $parr['af_furl'],
                'af_menuid' => $parr['admin_menu_id'],
                'af_strte'  => $parr['af_strte']
            );
            $b=$db->insert($arr);
            if($b){
                $url      = config('tempredirect_url').'adac/junior/admin_menu_id/'.$parr['am_pid'];
                $response = messageInfo('11000','添加成功','',$url);
            }else{
                $response = messageInfo('10008','添加失败');
            }
        }
        return $response;
    }

    //三级功能详情单个
    function fuInfoData($where){
        $admin_function =Db::name('admin_function');
        $data=$admin_function->where($where)->find();
        return $data;
    }

    //三级功能删除
    function fuDeteleApi($admin_function_id){
        $admin_function=Db::name('admin_function');
        $where['admin_function_id']=$admin_function_id;
        $b=$admin_function->where($where)->delete();
        if($b){
            $response = messageInfo('11000','删除成功');
        }else{
            $response = messageInfo('10008','删除失败');
        }
        return $response;
    }

    //角色列表
    function roleList(){
        $db = Db::name('admin_role');
        $Admin_user_lvtuan = session('Admin_user_lvtuan');
        if($Admin_user_lvtuan['a_role'] == 2){
            $where = null;
        }else if($Admin_user_lvtuan['a_role'] == 3){
            $where['role_id'] = array('not in','2,3');
        }
        $paginate = 40;
        $info = $db->where($where)->paginate($paginate);
        $lists_data = $info->toArray();
        $page         =  $info->render();
        $data['list'] = $lists_data['data'];
        $data['page'] = $page;// 分页显示输出*/
        return  $data;
    }



    //角色详情
    function roleData($role_id){
        $admin_role = Db::name('admin_role');
        $where['role_id'] = $role_id;
        $data       = $admin_role->where($where)->find();
        return $data;
    }

    //角色新增与编辑
    function rolesHandleApi($input){
        if(empty($input['r_username'])){
            $response = messageInfo('10008','系统参数错误');
            return $response;
        }
        $admin_role = Db::name('admin_role');
        $where['r_username'] = $input['r_username'];
        $count = $admin_role->where($where)->count();
        if(empty($input['role_id'])){
            if($count >= 1){
                $response = messageInfo('10008','角色已存在');
                return $response;
            }
            $data_arr['r_username'] = $input['r_username'];
            $a = $admin_role->insert($data_arr);
            if($a){
                $url      = config('tempredirect_url').'adac/role_list';
                $response = messageInfo('11000','添加成功','',$url);
            }else{
                $response = messageInfo('10008','添加失败');
            }
        }else{
            if($count > 2){
                $response = messageInfo('10008','角色已存在');
                return $response;
            }
            $a = $admin_role->where('role_id = '.$input['role_id'])->setField('r_username',$input['r_username']);
            if($a !== false){
                $url      = config('tempredirect_url').'adac/role_list';
                $response = messageInfo('11000','编辑成功','',$url);
            }else{
                $response = messageInfo('10008','编辑失败');
            }
        }
        return $response;
    }


    //角色删除
    function rolesDeleteApi($role_id)
    {
        if (empty($role_id)) {
            $response = messageInfo('10008', '系统参数错误');
            return $response;
        }
        $admin_role = Db::name('admin_role');
        $admin_role->startTrans();
        $del = Db::table('lt_admin_role_function')->where('admin_role_id='.$role_id)->delete();
        if(!$del){
            $admin_role->rollback();
            $response = messageInfo('10008', '删除失败');
            return $response;
        }

        $where['role_id'] = $role_id;
        $a = $admin_role->where($where)->delete();
        if ($a) {
            $admin_role->commit();
            $response = messageInfo('11000', '删除成功');
        } else {
            $admin_role->rollback();
            $response = messageInfo('10008', '删除失败');
        }
        return $response;
    }

    //角色信息
    function roleListAdd(){
        $Admin_user_lvtuan = session('Admin_user_lvtuan');
        if($Admin_user_lvtuan['a_role'] == 2){
            $where = null;
        }else if($Admin_user_lvtuan['a_role'] == 3){
            $where['role_id'] = array('not in','2,3');
        }
        $admin_role = Db::name('admin_role');
        $data = $admin_role->where($where)->select();
        return $data;
    }

}
